<template>
  <div class="login_container">
    <div class="login_box">
      <!-- 头像区域 -->
      <div class="avatar_box">
        <img src="@/assets/img/logo.png" alt="" />
      </div>
      <!-- 登录表单区域 -->
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="loginFormRef"
        label-width="0px"
        class="login_form"
      >
        <el-form-item prop="username">
          <el-input
            v-model="ruleForm.username"
            prefix-icon="el-icon-user-solid"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="ruleForm.password"
            prefix-icon="el-icon-lock"
            type="password"
          ></el-input>
        </el-form-item>
        <!-- 登录按钮区域 -->
        <el-form-item class="btns">
          <el-button type="primary" @click="loginForm()">登录</el-button>
          <el-button type="info" @click="resetForm('loginFormRef')"
            >重置</el-button
          >
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script type="text/ecmascript-6">
import {login} from '@/network/login.js'
import {setToken} from '@/utils/token.js'

export default {
  name: 'Login',
  data() {
    return {
      ruleForm:{
        username: 'admin',
        password: '123456'
      },

      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 2, max: 10, message: '长度在 2 到 10 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 5, max: 10, message: '长度在 5 到 10 个字符', trigger: 'blur' }
        ],
      }
    }
  },
  methods: {
    // 点击登录请求
    loginForm(){
      this.$refs.loginFormRef.validate((valid) => {
        if (!valid) return;
        login(this.ruleForm).then(res => {
          // console.log(res);
          const {data,meta} = res;
          if(meta.status !== 200) return this.$message.error('登录失败！');
          this.$message.success('登录成功！');
          // 存储token，跳转到主页
          setToken(data.token)
          this.$router.push('/home')
        })
      });
    },
    // 重置表单信息
    resetForm(formName){
      this.$refs[formName].resetFields();
    }
  },
}
</script>

<style lang="less" scoped>
.login_container {
  height: 100%;
  background-color: #2b4b6b;
  .login_box {
    width: 450px;
    height: 300px;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    background-color: #fff;
    border-radius: 3px;
    .avatar_box {
      width: 130px;
      height: 130px;
      border: 1px solid #eee;
      background-color: #fff;
      box-shadow: 0px 0px 6px 0px #ddd;
      border-radius: 50%;
      padding: 10px;
      position: relative;
      left: 50%;
      transform: translate(-50%, -50%);
      img {
        width: 100%;
        height: 100%;
        border-radius: 50%;
        background-color: #eee;
      }
    }
    .login_form {
      position: absolute;
      bottom: 0;
      width: 100%;
      padding: 0 20px;
      box-sizing: border-box;
      .btns {
        display: flex;
        justify-content: flex-end;
      }
    }
  }
}
</style>
